import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/*public class Test {
    public static void main(String[] args) throws IOException {
        try(OutputStream outputStream = new FileOutputStream("d:/test.txt",true)){
            PrintWriter writer = new PrintWriter(outputStream);
            writer.write("hello");
            writer.flush();
        }
    }
}*/
/*    public static void main(String[] args) throws IOException {
        try(InputStream inputStream = new FileInputStream("d:/test.txt")){
            Scanner scanner = new Scanner(inputStream);
            String s = scanner.next();
            System.out.println(s);
        }
    }*/

/*    public static void main(String[] args) {
        try(InputStream inputStream = new FileInputStream("d:/test.txt")){
            byte[] bytes = new byte[1024];
            int n = inputStream.read(bytes);
            System.out.println(n);
            for (int i = 0; i <n ; i++) {
                System.out.printf("%x ",bytes[i]);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }*/
/*    public static void main(String[] args) throws IOException {
        try(Writer writer = new FileWriter("d:/test.txt",true)){
            writer.write("我在学习文件io");
        }
    }*/
/*
  try(Reader reader = new FileReader("d:/test.txt")){
          while(true){
          char[] cubf = new char[1024];
          int n = reader.read(cubf);
          if(n==-1){
          break;
          }
          System.out.println(n);
          for (int i = 0; i <n ; i++) {
        System.out.print(cubf[i]);
        }
        }
        }*/
/*    public static void main1(String[] args) throws IOException {
        try(Reader reader = new FileReader("d:/test.txt")) {
            while (true){
                int c = reader.read();
                if(c==-1){
                    break;
                }
                char ch = (char)c;
                System.out.println(ch);
            }
        }
    }*/


/*    public static void main2(String[] args) {
        File f1 = new File("d:/test.txt");
        File f2 = new File("d:/test2.txt");
        boolean ret = f1.renameTo(f2);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        File f = new File("d:/");
        String[] files = f.list();
        System.out.println(Arrays.toString(files));
    }*/


/*public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径");
        String path = scanner.next();
        File rootPath = new File(path);
        if(!rootPath.isDirectory()){
            System.out.println("你输入的目录有误");
            return;
        }
        System.out.println("请输入你要删除的文件的关键词");
        String word = scanner.next();
        scanDir(rootPath,word);
    }

    private static void scanDir(File rootPath, String word){
        File[] files = rootPath.listFiles();
        if(files==null){
            return;
        }
        for(File f:files){
            System.out.println("当前扫描文件: "+f.getAbsolutePath());
            if(f.isFile()){
                checkDelete(f,word);
            }else{
                scanDir(f,word);
            }
        }
    }
    private static void checkDelete(File f,String word){
        if(!f.getName().contains(word)){
            return;
        }
        System.out.println("当前要删除的文件: "+f.getAbsolutePath()+"请确认是否要删除(Y/y)");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.next();
        if(choice.equals("Y")||choice.equals(("y"))){
            f.delete();
            System.out.println("删除完毕");
        }else{
            System.out.println("取消删除");
        }
    }
}*/

/*
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件的路径");
        String sourcePath = scanner.next();
        File sourceFile = new File(sourcePath);
        if (!sourceFile.exists()) {
            System.out.println("文件不存在");
            return;
        }
        if (!sourceFile.isFile()) {
            System.out.println("不是普通文件");
            return;
        }

        System.out.println("请输入要复制到的文件的路径");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        if (destFile.exists()) {
            if (destFile.isDirectory()) {
                System.out.println("这是路径，请检查是否输入正确");
                return;
            }
            if (destFile.isFile()) {
                System.out.println("目标路径已经存在，请确认是否覆盖？Y/y");
                Scanner scanner1 = new Scanner(System.in);
                String s = scanner1.next();
                if (!s.equals("y") && !s.equals("Y")) {
                    System.out.println("停止覆盖");
                    return;
                }
            }
        }

        try (OutputStream os = new FileOutputStream(destFile)){
            try (InputStream is = new FileInputStream(sourceFile)) {
                  byte[] buf = new byte[1024];
                  int len;
                  while(true){
                      len = is.read(buf);
                      if(len==-1){
                          break;
                      }
                      os.write(buf,0,len);
                  }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("复制已完成");
    }
}
*/

public class Test {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径");
        String rootPath =scanner.next();
        File root = new File(rootPath);
        if(!root.isDirectory()){
            System.out.println("你输入的不是目录");
            return;
        }
        System.out.println("请输入你要查找的字符");
        String token = scanner.next();
        List<File> result = new ArrayList<>();
        scan(root,token,result);
        System.out.println("找到了符合条件的文件"+result.size());
        for (File file:result) {
            System.out.println(file.getAbsolutePath());
        }
    }

    private static void scan(File root, String token, List<File> result ) throws IOException {
        File[] files = root.listFiles();
        if(files==null||files.length==0){
            return;
        }
        for (File file:files) {
            if(file.isDirectory()){
                scan(file,token, result);
            }else {
                if(Contains(file,token)){
                    result.add(file.getAbsoluteFile());
                }
            }
        }
    }
    private static boolean Contains(File file,String token) throws IOException {
        StringBuilder sb = new StringBuilder();
        try(InputStream is = new FileInputStream(file)){
            try(Scanner scanner = new Scanner(is,"UTF-8")){
                while(scanner.hasNextLine()){
                    sb.append(scanner.nextLine());
                    sb.append("\r\n");
                }
            }
        }
        return sb.indexOf(token)!=-1;
    }
}





















